package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.Collect;
import org.example.musicserver.mapper.CollectMapper;
import org.example.musicserver.service.CollectService;
import org.example.musicserver.service.MVService;
import org.example.musicserver.service.PlayListService;
import org.example.musicserver.service.SongService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Resource
    private CollectMapper collectMapper;

    @Resource
    private MVService mvService;

    @Resource
    private SongService songService;

    @Resource
    private PlayListService playListService;

    @Override
    public Result addSongCollection(Integer userId,Integer songId,Byte type) {
        //作者用type来判断收藏的是歌还是歌单
        Collect collect = new Collect();
        // 然后，设置userId和songId
        collect.setUserId(userId);
        collect.setLikeId(songId);
        collect.setType(type);
        if (collectMapper.insert(collect) > 0) {
            return Result.success("收藏成功", true);
        } else {
            return Result.error("收藏失败");
        }
    }

    @Override
    public Result deleteSongCollect(Integer userId, Integer songId,Byte type) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("like_id",songId);
        queryWrapper.eq("type",type);
        if (collectMapper.delete(queryWrapper) > 0) {
            return Result.success("取消收藏", true);
        } else {
            return Result.error("取消收藏失败");
        }
    }

    @Override
    public Result collectionSongOfUser(Integer userId,Byte type) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("type",type);
        queryWrapper.isNotNull("like_id");
        queryWrapper.select("like_id");//仅返回该字段
//        List<Collect> collects = collectMapper.selectList(queryWrapper);
//        List<Integer> songIds = collects.stream().map(Collect::getSongId).collect(Collectors.toList());
        List<Collect> collects = collectMapper.selectList(queryWrapper);

        List<Integer> ids = collects.stream().map(Collect::getLikeId).collect(Collectors.toList());
        Result result = null;
        if(ids.size() == 0){
            return Result.success("用户收藏", ids);
        }

        if(type == 1){
            return  songService.songOfSingerIds(ids);
        }else if(type == 2){
            return playListService.selectByIds(ids);
        }else if(type == 3){
            return mvService.selectByMvIds(ids);
        }

        return Result.success("用户收藏", ids);
    }

    @Override
    public Result collectionSongDetailOfUser(Integer userId,Byte type) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("type",type);
        queryWrapper.isNotNull("like_id");
        queryWrapper.select("like_id");//仅返回该字段
//        List<Collect> collects = collectMapper.selectList(queryWrapper);
//        List<Integer> songIds = collects.stream().map(Collect::getSongId).collect(Collectors.toList());
        List<Collect> collects = collectMapper.selectList(queryWrapper);

        return Result.success("用户收藏", collects);
    }
}
